using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.ReadApi.Application.Command;
using BaseData.ReadApi.Application.Command.Permissions;
using BaseData.ReadApi.Application.Command.Roles;
using BaseData.ReadApi.Application.Command.Users;
using BaseData.ReadApi.Dto;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.ReadApi.Controllers
{
    [Route("api/[controller]/[action]")] // 路由配置
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ManagementController> logger;

        public ManagementController(IMediator _mediator,ILogger<ManagementController> _logger)
        {
            mediator = _mediator;
            logger = _logger;
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<ResUserOutput>> LogingAsync([FromQuery]LogingCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<List<ResItemOutput>>> GetRoleItemAsync()
        {
            try
            {
                var cmd=new GetRoleItemCommand();
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<List<ResItemOutput>>> GetPermissionItemAsync()
        {
            try
            {
                var cmd = new GetPermissionItemCommand();
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<Apipaging<List<Role>>>> GetRoleAsync([FromQuery] GetRoleCommand cmd)
        {
            try
            {
                
                return await mediator.Send(cmd);  
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<Apipaging<List<Permission>>>> GetPermissionAsync([FromQuery] GetPermissionCommand cmd)
        {
            try
            {

                return await mediator.Send(cmd);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<Apipaging<List<UserInfo>>>> GetUserAsync([FromQuery] GetUserCommand cmd)
        {
            try
            {

                return await mediator.Send(cmd);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<Apiresult<ResUserOutput>> RefreshTokenAsync([FromBody] RefreshTokenCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="cmd">包含用户ID的命令</param>
        /// <returns>返回用户详情</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<UserDetailOutput>> GetUserDetailAsync([FromQuery] GetUserDetailCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取角色详情
        /// </summary>
        /// <param name="cmd">包含角色ID的命令</param>
        /// <returns>返回角色详情</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<RoleDetailOutput>> GetRoleDetailAsync([FromQuery] GetRoleDetailCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取权限详情
        /// </summary>
        /// <param name="cmd">包含权限ID的命令</param>
        /// <returns>返回权限详情</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Apiresult<PermissionDetailOutput>> GetPermissionDetailAsync([FromQuery] GetPermissionDetailCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
